﻿using HR.Config;
using HR.Filters;
using HR.Models;
using Microsoft.AspNetCore.Mvc;

namespace HR.Controllers
{
    public class MailTestController : Controller
    {
        private readonly IWebHostEnvironment webHostEnvironment;

        public MailTestController(IWebHostEnvironment webHostEnvironment)
        {
            this.webHostEnvironment = webHostEnvironment;
        }
        [TypeFilter(typeof(RoleFilterAttribute))]
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// post发送多条，调用邮箱的接口发送邮件
        /// </summary>
        /// <param name="mail"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult mailPosyForm([FromForm] Mail mail)
        {
            //将传入的字符串重新进行拆分操作
            mail.recipientArry = mail.recipientArry[0].Split(",");
            MailServer mc = new MailServer();
            return Json(mc.SendEmail(mail));
        }
        [TypeFilter(typeof(RoleFilterAttribute))]
        /// <summary>
        /// 文件上传视图小案例
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult IndexFileDemo()
        {
            return View();
        }
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult uploadFile()
        {
            //此处这里需要使用var来接收，前台表单值
            var files = Request.Form.Files;
            //这里来接收上传的图片名字
            List<string> fileNames = new List<string>();
            foreach (IFormFile item in files)
            {
                string name = DateTime.Now.ToString("yyyyMMddHHmmms");
                fileNames.Add(name);
                string ext = item.FileName.Substring(item.FileName.LastIndexOf('.'));
                //相对路径
                //string path = $"uploaders/{DateTime.Now.ToString("yyyy/MM/dd/")}" + name + ext;
                string path = $"uploaders/" + name + ext;
                //绝对路径
                string jpath = Path.Combine(webHostEnvironment.WebRootPath, path);
                //创建上传文件对应的文件夹
                (new FileInfo(jpath)).Directory.Create();
                using (FileStream fileStream = new FileStream(jpath, FileMode.CreateNew))
                {
                    item.CopyTo(fileStream);//文件上传
                                            // string? id = hf.human_id;
                    TempData["name"] = name + ext;//保存图片的值
                    //human_file hf = new human_file
                    //{
                    //    human_picture=path,
                    //};
                    //ViewBag.Model = JsonConvert.SerializeObject(hf);
                }
            }
            //return Content("花花的世界是主角是主角");
            return Json(fileNames);
        }
        /// <summary>
        /// 通过第三种钩子函数上传的文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public IActionResult uploadImg(IFormFileCollection file)
        {
            string path = "";
            foreach (var item in file)
            {
                string name = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string ext = item.FileName.Substring(item.FileName.LastIndexOf('.'));

                path = $"ZiJiImg/" + name + ext;
                string jpath = Path.Combine(webHostEnvironment.WebRootPath, path);
                //(new FileInfo(jpath)).Directory.Create();
                using (FileStream filel = new FileStream(jpath, FileMode.CreateNew))
                {
                    item.CopyTo(filel);
                }

            }
            return Json(path);
        }
    }
}
